

#include "systick.h"
#include "turn_LED.h"

#include "user_struct.h"

uint16_t LED_turn_on_time[2] = {0, 0};
uint16_t LED_turn_off_time[2] = {0, 0};

/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

FlagStatus turnLED_blink_ctrl(ENUM_TURN_LED num, uint8_t status, uint16_t blink_time_ms)
{
	FlagStatus gpio_output;
	
	uint16_t turn_on_time = LED_turn_on_time[num];
	uint16_t turn_off_time = LED_turn_off_time[num];
	
	uint16_t now_time = (uint16_t)get_system_runingtime();

	if(status == 1)
	{
		
		if((uint16_t)(now_time - turn_on_time) < blink_time_ms)
		{
			gpio_output = SET;
			turn_off_time = now_time;
		}
		else 
		{
			if((uint16_t)(now_time - turn_off_time) > blink_time_ms)
			{
				turn_on_time = now_time;
			}
			
			gpio_output = RESET;
		}
	}
	
	else
	{
		gpio_output = RESET;
		turn_on_time = 0;
		turn_off_time = 0;
	}
	
	LED_turn_on_time[num] = turn_on_time;
	LED_turn_off_time[num] = turn_off_time;
	
	return gpio_output;
}




/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/

void turn_led_ctrl(void)
{
	
	userHdl.ctrl.out_turn_left  = turnLED_blink_ctrl(LED_LEFT, userHdl.ctrl.en_turn_left, LED_BLINK_TIME_MS);
	userHdl.ctrl.out_turn_right = turnLED_blink_ctrl(LED_RIGHT, userHdl.ctrl.en_turn_right, LED_BLINK_TIME_MS);
}



